package com.example.View.Renderers;

import java.util.HashMap;
import java.util.Map;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.example.R;
import com.example.View.Projection;
import com.example.models.Building;
import com.example.util.Figure;

public class BuildingRenderer extends FigureRendererBase {

	// The paint to be used for rendering buildings
	private Paint normalPaint;

	private Map<String, Bitmap> iconByName;

	/**
	 * Constructs a BuildingRenderer
	 */
	public BuildingRenderer(Resources resources) {
		super(resources);
		normalPaint = new Paint();
		normalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
		normalPaint.setARGB(255, 255, 255, 0);
		normalPaint.setAntiAlias(true);
		iconByName = new HashMap<String, Bitmap>();
		iconByName.put("CentralBase", createBitmap(R.drawable.central_base));
		iconByName.put("GoblinTavern", createBitmap(R.drawable.goblin_tavern));
		iconByName.put("PhoneixNest", createBitmap(R.drawable.phoneix_nest));
		iconByName.put("TankWorkshop", createBitmap(R.drawable.tank_factory));
		iconByName.put("TrollLair", createBitmap(R.drawable.troll_label));
		iconByName.put("DeafultBuildingName", createBitmap(R.drawable.building_label));
	}

	/**
	 * Renders a given figure
	 * @param canvas The canvas on which (and using which) this figure is rendered
	 * @param projection The translation between the model coordinate system and the view coordinate system.
	 * @param figure The figure to be rendered.
	 */
	@Override
	public void render(Canvas canvas, Projection projection, Figure figure) {
		Building building = (Building)figure;
		Bitmap bitmap = getBitmap(building);
		canvas.drawBitmap(bitmap, figure.getX() * projection.getPixelsPerCell() - projection.getX() - ((bitmap.getWidth() / 2) - (projection.getPixelsPerCell() / 2)), figure.getY() * projection.getPixelsPerCell() - projection.getY() - getBitmap(building).getHeight() + projection.getPixelsPerCell(), normalPaint);
	}

	/**
	 * Returns the bitmap to be used for a given building according to player
	 */
	public Bitmap getBitmap(Building building) {
		String name = building.getName();
		Bitmap bitmap = iconByName.get(name);
		if (building.getPlayerId() == 2) {
        	bitmap = Bitmap.createScaledBitmap(bitmap, -bitmap.getWidth(), bitmap.getHeight(), true);
        }
		return (bitmap);
	}


}